package com.fu.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 付大大
 * @description
 * @create 2021-01-25 11:13
 */
@WebServlet("/servletDemo01")
public class ServletDemo01 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.write("欢迎本网站，您的最后范文时间为：<br>");

        Cookie cookie = new Cookie("time", System.currentTimeMillis() + "");
        cookie.setMaxAge(3600);
        resp.addCookie(cookie);

        Cookie[] cookies = req.getCookies();
        for (Cookie cookie1 : cookies) {
            if ("time".equals(cookie1.getName())) {
                String value = cookie1.getValue();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                writer.write(simpleDateFormat.format(new Date(Long.parseLong(value))));
            }
        }




    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
